//配置文件
let config = {
    //文章列表
    postList: "config/posts.json",
    //菜单——关于
    about: "./layouts/menu/About.md",
    //菜单——联系
    contact: "./layouts/menu/Contact.md",
    //菜单——首页
    home: "./layouts/menu/Home.md",
    //菜单——错误页
    notFound: "./layouts/menu/NotFound.md"
}

let navUrl = {
    Home: './index.html',
    Aboud: './about.html',
    Contact: './contact.html'
}

// 文章列表
let postList;

// 菜单导航
function addNav() {
    let nav = document.createElement('div');
    nav.setAttribute('id', "nav");
    for (let i in navUrl) {
        let a = document.createElement('a');
        a.innerHTML = i;
        a.setAttribute('href', navUrl[i]);
        nav.appendChild(a);
    }
    document.getElementById("content").appendChild(nav);
}


// http请求文件，曲线救国读取文件内容
function request(fileUrl, callback) {
    let xhr = new XMLHttpRequest();
    xhr.open('GET', fileUrl);
    xhr.send();
    xhr.onload = function() {
        if (xhr.status != 200) {
            notFound();
        } else {
            callback(xhr.responseText);
            addNav();
        }
    };
    xhr.onerror = function() {
        notFound();
    };
}

// markdown转html，并写入页面
function readPost(responseText) {
    document.getElementById("content").innerHTML = marked(responseText);
}

// 跳转错误页
function notFound() {
    window.location.href = './notfound.html';
}